﻿/********************************************************************
	日期:2022/5/1 12:31
	名称:MainCitySys
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:主城业务系统
*********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PEProtocol;
using UnityEngine;
using UnityEngine.AI;

public class MainCitySys : SystemRoot{
    public static MainCitySys Instance = null;

    public GuideWnd guideWnd;
    
    public MainCityWnd mainCityWnd;

    public BuyWnd buyWnd;
    
    public StrongWnd strongWnd;

    public ChatWnd chatWnd;

    public InfoWnd infoWnd;

    public TaskWnd taskWnd;

    private PlayerController playerController;

    private Transform charCamTrans;

    private AutoGuideCfg curTaskData;

    private Transform[] npcPosTrans;

    private NavMeshAgent nav;

    public override void InitSys(){
        base.InitSys();
        Instance = this;
        PECommon.Log("Init MainCitySys...");
    }

    #region FuBenSys

    public void EnterFuBen(){
        StopNavTask();
        FuBenSys.Instance.EnterFuBen();
    }
    
    #endregion

    public void EnterMainCity(){
        MapCfg mapData = resSvc.GetMapCfgData(Constants.MainCityMapID);
        resSvc.AsyncLoadScene(mapData.sceneName, () => {
            PECommon.Log("Enter MainCity...");

            //加载游戏主角
            LoadPlayer(mapData);

            //打开主城场景UI
            mainCityWnd.SetWndState();

            //播放主城背景音乐
            audioSvc.PlayBGMusic(Constants.BGMainCity);

            GameObject map = GameObject.FindGameObjectWithTag("MapRoot");
            MainCityMap mcm = map.GetComponent<MainCityMap>();
            npcPosTrans = mcm.NpcPosTrans;
            //TODO 设置人物展示相机
            if (charCamTrans != null){
                charCamTrans.gameObject.SetActive(false);
            }
        });
    }

    #region TaskWnd

    public void OpenTaskWnd(){
        taskWnd.SetWndState();
    }

    #endregion
    
    #region chatWnd

    public void OpenChatWnd(){
        chatWnd.SetWndState();
    }

    #endregion

    #region StrongWnd

    public void OpenStrongWnd(){
        StopNavTask();
        strongWnd.SetWndState();
    }

    #endregion

    #region player
    private void LoadPlayer(MapCfg cfg){
        GameObject player = resSvc.LoadPrefab(PathDefine.AssissnCityPlayerPrefab, true);
        player.transform.position = cfg.playerBornPos;
        player.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
        
        //相机初始化
        Camera.main.transform.position = cfg.mainCamPos;
        Camera.main.transform.localEulerAngles = cfg.mainCamRote;

        playerController = player.GetComponent<PlayerController>();
        playerController.Init();
        nav = player.GetComponent<NavMeshAgent>();
    }

    public void SetMoveDir(Vector2 dir){
        StopNavTask();
        if (dir == Vector2.zero){
            playerController.SetBlend(Constants.BlendIdle);
        }
        else{
            playerController.SetBlend(Constants.BlendMove);
        }

        playerController.Dir = dir;
    }

    public void setShowCharCaramePos(){
        if (!infoWnd.enabled){
            return;
        }
        if (charCamTrans == null){
            charCamTrans = GameObject.FindGameObjectWithTag("ShowCharCamera").transform;
        }
        //设置人物展示相机相对位置
        charCamTrans.localPosition = playerController.transform.position + playerController.transform.forward * 2.8f + new Vector3(0, 1.2f, 0);
        charCamTrans.localEulerAngles = new Vector3(0, 180 + playerController.transform.localEulerAngles.y, 0);
        charCamTrans.localScale = Vector3.one;
        charCamTrans.gameObject.SetActive(true);
    }

    private float startRotate = 0;
    public void SetStartRotate(){
        startRotate = playerController.transform.localEulerAngles.y;
    }

    public void SetPlayerRotate(float rotate){
        playerController.transform.localEulerAngles = new Vector3(0, startRotate + rotate, 0);
    }
    
    #endregion

    #region infoWnd

    public void OpenInfoWnd(){
        StopNavTask();
        this.setShowCharCaramePos();
        infoWnd.SetWndState();
    }

    public void CloseInfoWnd(){
        if (charCamTrans != null){
            charCamTrans.gameObject.SetActive(false);
        }
        infoWnd.SetWndState(false);
    }
    
    #endregion

    #region BuyWnd
    
    public void OpenBuyWnd(int type){
        StopNavTask();
        buyWnd.SetBuyType(type);
        buyWnd.SetWndState();
    }

    #endregion
    
    #region Guide

    private bool isNavGuide = false;
    public void RunTask(AutoGuideCfg agc){
        if (agc != null){
            curTaskData = agc;
        }
        //解析任务数据，执行相关操作
        nav.enabled = true;
        if (curTaskData.npcID != -1){
            float dis = Vector3.Distance(playerController.transform.position, npcPosTrans[agc.npcID].position);
            if (dis < 0.5){
                isNavGuide = false;
                nav.isStopped = true;
                playerController.SetBlend(Constants.BlendIdle);
                nav.enabled = false;
                OpenGuideWnd();
            }
            else{
                nav.enabled = true;
                nav.speed = Constants.PLayerMoveSpeed;
                nav.SetDestination(npcPosTrans[agc.npcID].position);
                playerController.SetBlend(Constants.BlendMove);
                isNavGuide = true;
            }
        }
        else{
            OpenGuideWnd();
        }
    }

    private void Update(){
        if (isNavGuide){
            IsArriveNavPos();
            playerController.SetCam();
        }
    }

    private void IsArriveNavPos(){
        float dis = Vector3.Distance(playerController.transform.position, npcPosTrans[curTaskData.npcID].position);
        if (dis < 0.5){
            isNavGuide = false;
            nav.isStopped = true;
            playerController.SetBlend(Constants.BlendIdle);
            nav.enabled = false;
            OpenGuideWnd();
        }
        else{
            nav.enabled = true;
            nav.speed = Constants.PLayerMoveSpeed;
            nav.SetDestination(npcPosTrans[curTaskData.npcID].position);
            playerController.SetBlend(Constants.BlendMove);
            isNavGuide = true;
        }
    }
    
    private void StopNavTask(){
        if (isNavGuide){
            isNavGuide = false;
            nav.isStopped = true;
            nav.enabled = false;
            playerController.SetBlend(Constants.BlendIdle);
        }
    }
    
    private void OpenGuideWnd(){
        guideWnd.SetWndState();
    }

    public AutoGuideCfg GetCurTaskData(){
        return curTaskData;
    }

    public void ResGuide(GameMsg msg){
        ResGuide data = msg.resGuide;
        GameRoot.AddTips(Constants.Color("任务奖励 金币+" + curTaskData.coin + " 经验+" + curTaskData.exp, TxtColor.Blue));
        switch (curTaskData.actID){
            case 0:
                //与智者对话
                break;
            case 1:
                //TODO 进入副本
                EnterFuBen();
                break;
            case 2:
                //TODO 进入强化界面
                OpenStrongWnd();
                break;
            case 3:
                //TODO 进入体力购买
                OpenBuyWnd(0);
                break;
            case 4:
                //TODO 进入金币铸造
                OpenBuyWnd(1);
                break;
            case 5:
                //TODO 进入世界聊天
                OpenChatWnd();
                break;
        }
        GameRoot.Instance.SetPlayerDataByGuide(data);
        mainCityWnd.RefreshUI();
    }
    #endregion

    public void ResStrong(GameMsg msg){
        int zhanliPre = PECommon.GetFightByProps(GameRoot.Instance.PlayerData);
        GameRoot.Instance.UpdatePlayerDataByStrong(msg.resStrong);
        int zhanliNow = PECommon.GetFightByProps(GameRoot.Instance.PlayerData);
        GameRoot.AddTips(Constants.Color("战力值提升" + (zhanliNow - zhanliPre), TxtColor.Blue));
        strongWnd.UpdateUI();
        mainCityWnd.RefreshUI();
    }

    public void PshChat(GameMsg msg){
        chatWnd.AddChatMsg(msg.pshChat.name, msg.pshChat.chat);
    }

    public void ResBuy(GameMsg msg){
        ResBuy data = msg.resBuy;
        GameRoot.Instance.UpdatePlayerDataByBuy(data);
        GameRoot.AddTips("购买成功");
        mainCityWnd.RefreshUI();
        buyWnd.SetWndState(false);

        if (msg.pshTaskPrg != null){
            GameRoot.Instance.SetPlayerDataByTaskPsh(msg.pshTaskPrg);
            if (taskWnd.GetWndState()){
                taskWnd.RefreshUI();
            }
        }
    }

    public void PshPower(GameMsg msg){
        PshPower data = msg.pshPower;
        GameRoot.Instance.SetPlayerDataByPower(data);
        if (mainCityWnd.gameObject.activeSelf){
            mainCityWnd.RefreshUI();
        }
    }

    public void ResTakeTaskReward(GameMsg msg){
        ResTakeTaskReward data = msg.resTakeTaskReward;
        GameRoot.Instance.SetPlayerDataByTask(data);
        
        taskWnd.RefreshUI();
        mainCityWnd.RefreshUI();
    }

    public void PshTaskPrg(GameMsg msg){
        PshTaskPrg data = msg.pshTaskPrg;
        
        GameRoot.Instance.SetPlayerDataByTaskPsh(data);
        if (taskWnd.GetWndState()){
            taskWnd.RefreshUI();
        }
    }
}
